//
//  SCAlertView.m
//  SCFoundationKit
//
//  Created by Chao Sheng on 2023/10/17.
//

#import "SCAlertView.h"
#import "SCBasicKit.h"

@interface SCAlertView ()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) UIView *customView;

@property (nonatomic, copy) NSString *content;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, strong) NSArray *buttons;

@property (nonatomic, copy) void (^scCallback)(NSInteger index);

@end

@implementation SCAlertView

- (void)showAlertTitle:(NSString *)title Content:(NSString *)content Buttons:(NSArray *)buttons ClickCallBack:(void (^)(NSInteger))callback {
    
    _btnClickHidden = true;
    _title = title;
    _content = content;
    _buttons = buttons;
    _scCallback = callback;
    [self setupUI:false];
    
    [self show];
}

- (void)showAlertTitle:(NSString *)title CustomView:(UIView *)customView Buttons:(NSArray *)buttons ClickCallBack:(void (^)(NSInteger))callback {
    _customView = customView;
    _title = title;
    _buttons = buttons;
    _scCallback = callback;
    [self setupUI:true];
    
    [self show];
}

- (void)setupUI:(BOOL)custom {
    
    [self addSubview:self.titleLabel];
    self.titleLabel.text = _title;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@12);
        make.centerX.equalTo(@0);
    }];
    
    if(custom) {
        [self addSubview:self.customView];
        [self.customView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@40);
            make.left.right.equalTo(@0);
            make.bottom.equalTo(@-50);
        }];
    } else {
        [self addSubview:self.contentLabel];
        self.contentLabel.text = _content;
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(12);
            make.left.equalTo(@12);
            make.right.equalTo(@-12);
        }];
    }
    
    UIButton *last = nil;
    for(NSInteger i = 0; i<_buttons.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:_buttons[i] forState:UIControlStateNormal];
        [btn setTitleColor:sc_blueColor forState:UIControlStateNormal];
        btn.titleLabel.font = sc_PingFangRegularFont(16);
        btn.tag = 2000+i;
        [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            if(last) {
                make.left.equalTo(last.mas_right);
            } else {
                make.left.equalTo(@0);
            }
            make.bottom.equalTo(@0);
            make.height.equalTo(@50);
            make.width.equalTo(self).multipliedBy(1.0/self.buttons.count);
        }];
        last = btn;
        if(i==0) {
            UIView *line = [[UIView alloc] init];
            line.backgroundColor = [sc_grayColor colorWithAlphaComponent:0.5];
            [self addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(@-50);
                make.height.equalTo(@0.5);
                make.left.right.equalTo(@0);
            }];
        }
        
        if(i != _buttons.count - 1) {
            UIView *line = [[UIView alloc] init];
            line.backgroundColor = [sc_grayColor colorWithAlphaComponent:0.5];
            [self addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(btn);
                make.width.equalTo(@0.5);
                make.top.bottom.equalTo(btn);
            }];
        }
    }
}

- (void)show {
    UIView *maskView = [[UIView alloc] init];
    maskView.tag = 1024;
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [UIApplication.sharedApplication.sc_keyWindow addSubview:maskView];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(maskAction)];
    [maskView addGestureRecognizer:tap];
    
    self.layer.cornerRadius = 12;
    self.layer.masksToBounds = true;
    [UIApplication.sharedApplication.sc_keyWindow addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.centerY.equalTo(@0);
        make.width.equalTo(@(sc_w(300)));
        make.height.greaterThanOrEqualTo(self.mas_width).multipliedBy(0.6);
    }];
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
    
    self.style = _style;
}

- (void)hiddenAction {
    self.alpha = 1;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
    UIView *maskView = [UIApplication.sharedApplication.sc_keyWindow viewWithTag:1024];
    [maskView removeFromSuperview];
    maskView = nil;
    
    [UIApplication.sharedApplication.sc_keyWindow endEditing:YES];
}

- (void)clickAction:(UIButton*)sender {
    _scCallback(sender.tag-2000);
    if(_btnClickHidden) {
        [self hiddenAction];
    }
}

- (void)hide {
    [self hiddenAction];
}

- (void)maskAction {
    [UIApplication.sharedApplication.sc_keyWindow endEditing:YES];
}

- (void)setStyle:(SCAlertStyle)style {
    _style = style;
    if(_style == SCAlertStyleDark) {
        self.backgroundColor = sc_rgb(26, 27, 36);
        self.titleLabel.textColor = [UIColor.whiteColor colorWithAlphaComponent:0.8];
        self.contentLabel.textColor = UIColor.whiteColor;

    } else {
        self.backgroundColor = UIColor.whiteColor;
        self.titleLabel.textColor = UIColor.blackColor;//sc_blackColor;
        self.contentLabel.textColor = sc_blackColor;
    }
}

#pragma mark - 懒加载
- (UILabel *)titleLabel {
    if(!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = sc_PingFangMediumFont(16);
        _titleLabel.textColor = [UIColor.whiteColor colorWithAlphaComponent:0.8];
    }
    return _titleLabel;
}

- (UILabel *)contentLabel {
    if(!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = sc_PingFangRegularFont(15);
        _contentLabel.textColor = UIColor.whiteColor;
        _contentLabel.numberOfLines = 0;
        _contentLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _contentLabel;
}


@end
